package o;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface.OnKeyListener;
import android.os.Build.VERSION;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.Window.Callback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.core.view.ViewCompat;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@RestrictTo({d.ॱ})
public class hh {
    private static Method ˋ = null;
    private static boolean ˎ = false;
    private static Field ˏ = null;
    private static boolean ॱ = false;

    public interface c {
        boolean ˊ(KeyEvent keyEvent);
    }

    public static boolean ˊ(@NonNull View view, @NonNull KeyEvent keyEvent) {
        return ViewCompat.ˊ(view, keyEvent);
    }

    public static boolean ˋ(@NonNull c cVar, @Nullable View view, @Nullable Callback callback, @NonNull KeyEvent keyEvent) {
        if (cVar == null) {
            return false;
        }
        if (VERSION.SDK_INT >= 28) {
            return cVar.ˊ(keyEvent);
        }
        if (callback instanceof Activity) {
            return ॱ((Activity) callback, keyEvent);
        }
        if (callback instanceof Dialog) {
            return ˋ((Dialog) callback, keyEvent);
        }
        if ((view == null || !ViewCompat.ॱ(view, keyEvent)) && !cVar.ˊ(keyEvent)) {
            return false;
        }
        return true;
    }

    private static boolean ॱ(ActionBar actionBar, KeyEvent keyEvent) {
        if (!ॱ) {
            try {
                ˋ = actionBar.getClass().getMethod("onMenuKeyEvent", new Class[]{KeyEvent.class});
            } catch (NoSuchMethodException e) {
            }
            ॱ = true;
        }
        if (ˋ != null) {
            try {
                return ((Boolean) ˋ.invoke(actionBar, new Object[]{keyEvent})).booleanValue();
            } catch (IllegalAccessException e2) {
            } catch (InvocationTargetException e3) {
            }
        }
        return false;
    }

    private static boolean ॱ(Activity activity, KeyEvent keyEvent) {
        activity.onUserInteraction();
        Window window = activity.getWindow();
        if (window.hasFeature(8)) {
            ActionBar actionBar = activity.getActionBar();
            if (keyEvent.getKeyCode() == 82 && actionBar != null && ॱ(actionBar, keyEvent)) {
                return true;
            }
        }
        if (window.superDispatchKeyEvent(keyEvent)) {
            return true;
        }
        View decorView = window.getDecorView();
        if (ViewCompat.ॱ(decorView, keyEvent)) {
            return true;
        }
        return keyEvent.dispatch(activity, decorView != null ? decorView.getKeyDispatcherState() : null, activity);
    }

    private static OnKeyListener ˋ(Dialog dialog) {
        if (!ˎ) {
            try {
                ˏ = Dialog.class.getDeclaredField("mOnKeyListener");
                ˏ.setAccessible(true);
            } catch (NoSuchFieldException e) {
            }
            ˎ = true;
        }
        if (ˏ != null) {
            try {
                return (OnKeyListener) ˏ.get(dialog);
            } catch (IllegalAccessException e2) {
            }
        }
        return null;
    }

    private static boolean ˋ(Dialog dialog, KeyEvent keyEvent) {
        OnKeyListener ˋ = ˋ(dialog);
        if (ˋ != null && ˋ.onKey(dialog, keyEvent.getKeyCode(), keyEvent)) {
            return true;
        }
        Window window = dialog.getWindow();
        if (window.superDispatchKeyEvent(keyEvent)) {
            return true;
        }
        View decorView = window.getDecorView();
        if (ViewCompat.ॱ(decorView, keyEvent)) {
            return true;
        }
        return keyEvent.dispatch(dialog, decorView != null ? decorView.getKeyDispatcherState() : null, dialog);
    }
}
